//
//  LXOrderSegment.m
//  LXGlobalMall
//
//  Created by 王敏 on 16/6/3.
//  Copyright © 2016年 SJT. All rights reserved.
//

#import "LXOrderSegment.h"
@interface LXOrderSegment()

@property(nonatomic,strong)UIView * underLine;
@property(nonatomic,strong)NSArray<UIButton*>*optionButtons;

@property(nonatomic,strong)NSArray<UIView*>*separationlines;

@property(nonatomic,strong)UIView *underLineContentView;
@end

@implementation LXOrderSegment

-(id)initWithFrame:(CGRect)frame{
    if(self = [super initWithFrame:frame]){
        self.backgroundColor = [UIColor clearColor];
        
        UIView * underLine = [[UIView alloc] init];
        underLine.height = 2;
        underLine.backgroundColor = [UIColor blackColor];
        self.underLine = underLine;
        
        
        UIView * underLineContentView = [[UIView alloc] initWithFrame:CGRectMake(0, self.height, self.width, 1)];
        underLineContentView.backgroundColor = RGBColor(211, 209, 209);
        [self addSubview:underLineContentView];
        [underLineContentView addSubview:underLine];
        self.underLineContentView = underLineContentView;
        
        self.selectedColor = [UIColor redColor];
        self.defaultColor = [UIColor blackColor];
    }
    return self;
}


-(void)setOptions:(NSArray<NSString *> *)options{
    _options = options;
    CGFloat width = self.width/options.count;
    CGFloat height = self.height;
    UIFont * font = [UIFont systemFontOfSize:16];
    NSMutableArray * marr = [NSMutableArray array];
    NSMutableArray * separationlines = [NSMutableArray array];
    [options enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
        [self addSubview:button];
        button.backgroundColor = [UIColor clearColor];
        button.titleLabel.font = font;
        button.frame = CGRectMake(idx*width, 0, width, height);
        button.tag = idx;
        [button setTitle:obj forState:UIControlStateNormal];
        [button setTitleColor:self.defaultColor forState:UIControlStateNormal];
        [button setTitleColor:self.selectedColor forState:UIControlStateSelected];
        [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        [marr addObject:button];
        
        if(idx!=options.count -1){
            UIView * separationline = [[UIView alloc] initWithFrame:CGRectMake((idx+1)*(width), 0, 1, height)];
            separationline.backgroundColor = RGBColor(211, 211, 211);
            separationline.height = button.height*0.5;
            separationline.centerY = button.centerY;
            [self addSubview:separationline];
            separationline.hidden = YES;
            [separationlines addObject:separationline];
        }
    }];
    self.optionButtons = marr;
    self.separationlines = separationlines;
    UIButton * btn = marr[0];
    self.selectedIndex = 0;
    self.underLine.width = [btn.currentTitle sizeInAre:MaxSize font:btn.titleLabel.font].width;
    self.underLine.centerX = btn.centerX;
    self.underLine.y = btn.botton-2;
}

-(void)buttonClick:(UIButton*)button{
    self.selectedIndex = button.tag;
}

-(void)setSelectedIndex:(NSInteger)selectedIndex{
    if(selectedIndex<0){
        selectedIndex = 0;
    }
    if(selectedIndex>self.options.count-1){
        selectedIndex = self.options.count-1;
    }
    _selectedIndex = selectedIndex;
    for (int i = 0; i<self.optionButtons.count; i++) {
        UIButton * btn = self.optionButtons[i];
        btn.selected = selectedIndex==i?YES:NO;
        
    };

    UIButton * button = self.optionButtons[selectedIndex];
    [UIView animateWithDuration:0.2 animations:^{
        self.underLine.centerX = button.centerX;
    }completion:^(BOOL finished) {
        if(self.handle){
            self.handle(self.optionButtons[selectedIndex]);
        }
    }];

}

-(void)layoutSubviews{
    [super layoutSubviews];
    self.underLineContentView.botton = self.height;
    self.underLine.botton = self.underLineContentView.height;
}


-(void)setEnableUnderLine:(BOOL)enableUnderLine{
    _enableUnderLine = enableUnderLine;
    self.underLineContentView.backgroundColor = enableUnderLine? RGBColor(211, 209, 209):[UIColor clearColor];
}
-(void)setSliderColor:(UIColor *)sliderColor{
    _sliderColor = sliderColor;
    self.underLine.backgroundColor = sliderColor;

}


-(void)setSliderWidth:(CGFloat)sliderWidth{
    _sliderWidth = sliderWidth;
    self.underLine.width = sliderWidth;
    self.underLine.centerX = self.optionButtons[self.selectedIndex].centerX;
}

-(void)setSliderHeight:(CGFloat)sliderHeight{
    _sliderHeight = sliderHeight;
    self.underLineContentView.height = sliderHeight;
    self.underLine.height = sliderHeight;
    [self setNeedsLayout];
}


-(void)setSelectedColor:(UIColor *)selectedColor{
    _selectedColor = selectedColor;
    for (UIButton *  button in self.optionButtons) {
        [button setTitleColor:selectedColor forState:UIControlStateSelected];
    }
}

-(void)setDefaultColor:(UIColor *)defaultColor{
    _defaultColor = defaultColor;
    for (UIButton *  button in self.optionButtons) {
        [button setTitleColor:defaultColor forState:UIControlStateNormal];
    }
}


-(void)setEnableSlider:(BOOL)enableSlider{
    _enableSlider = enableSlider;
    self.underLine.hidden = !enableSlider;
}

-(void)setEnableSeparationline:(BOOL)enableSeparationline{
    _enableSeparationline = enableSeparationline;
    for (UIView * v in self.separationlines) {
        v.hidden = !enableSeparationline;
    }
}
@end
